﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace LprConfiger.Winform
{
    static class EndianConverter
    {
        public static uint ToHostOrder(this uint networkOrder)
        {
            return (uint) IPAddress.NetworkToHostOrder(networkOrder);
        }

        public static int ToHostOrder(this int networkOrder)
        {
            return IPAddress.NetworkToHostOrder(networkOrder);
        }

        public static ushort ToHostOrder(this ushort networkOrder)
        {
            return (ushort)IPAddress.NetworkToHostOrder(networkOrder);
        }

        public static CFGPolice FromBigEndian(ref CFGPolice police)
        {
            police.outputCtrl.jpegMaxKb = police.outputCtrl.jpegMaxKb.ToHostOrder();
            police.outputCtrl.pictureForm = (PICTURE_FORM) ((int) police.outputCtrl.pictureForm).ToHostOrder();

            for (int i = 0; i < police.lanes.Length; i++)
            {
                var lane = police.lanes[i];
                lane.dirLamps = lane.dirLamps.ToHostOrder();
                lane.directions = lane.directions.ToHostOrder();
            }

            police.allLamp.correctLevel = police.allLamp.correctLevel.ToHostOrder();
            police.allLamp.hwType = police.allLamp.hwType.ToHostOrder();
            police.allLamp.number = police.allLamp.number.ToHostOrder();
            for (int i = 0; i < police.allLamp.lamps.Length; i++)
            {
                var lamp = police.allLamp.lamps[i];
                lamp.greenDelay = lamp.greenDelay.ToHostOrder();
                lamp.shape = lamp.shape.ToHostOrder();
                for (int j = 0; j < lamp.regions.Length; j++)
                {
                    var r = lamp.regions[j];
                    r.x = r.x.ToHostOrder();
                    r.y = r.y.ToHostOrder();
                    r.width = r.width.ToHostOrder();
                    r.height = r.height.ToHostOrder();
                    lamp.regions[j] = r;
                }

                police.allLamp.lamps[i] = lamp;
            }

            police.eventDetect.forbiddenTypes = police.eventDetect.forbiddenTypes.ToHostOrder();
            police.eventDetect.minification = police.eventDetect.minification.ToHostOrder();
            for (int i = 0; i < police.eventDetect.lines.Length; i++)
            {
                var line = police.eventDetect.lines[i];
                line.start.x = line.start.x.ToHostOrder();
                line.start.y = line.start.y.ToHostOrder();
                line.end.x = line.end.x.ToHostOrder();
                line.end.y = line.end.y.ToHostOrder();
                police.eventDetect.lines[i] = line;
            }

            var buffer = BitConverter.GetBytes(police.reco.ratio);
            var reversed = buffer.Reverse().ToArray();
            police.reco.ratio = BitConverter.ToSingle(reversed, 0);
            return police;
        }

        public static CFGCommon FromBigEndian(ref CFGCommon data)
        {
            for (int i = 0; i < data.baseInfo.limitSpeed.Length; i++)
            {
                data.baseInfo.limitSpeed[i] = data.baseInfo.limitSpeed[i].ToHostOrder();
            }

            data.sysCtrl.intervalFlowReport = data.sysCtrl.intervalFlowReport.ToHostOrder();

            data.sysService.dataPort = data.sysService.dataPort.ToHostOrder();
            data.sysService.debugPort = data.sysService.debugPort.ToHostOrder();
            data.sysService.configPort = data.sysService.configPort.ToHostOrder();

            data.camera.ctrl.imageHeight = data.camera.ctrl.imageHeight.ToHostOrder();
            data.camera.ctrl.imageWidth = data.camera.ctrl.imageWidth.ToHostOrder();
            data.camera.ctrl.videoHeight = data.camera.ctrl.videoHeight.ToHostOrder();
            data.camera.ctrl.videoWidth = data.camera.ctrl.videoWidth.ToHostOrder();
            data.camera.ctrl.videoRate = data.camera.ctrl.videoRate.ToHostOrder();
            for (var i = 0; i < data.camera.ctrl.strategyNum; i++)
            {
                var strategyItem = data.camera.ctrl.items[i];
                for (var j = 0; j < data.camera.ctrl.strategyItemNum; j++)
                {
                    strategyItem.items[j] = (CAMERA_CTRL) ((int) strategyItem.items[j]).ToHostOrder();
                    strategyItem.value[j] = strategyItem.value[j].ToHostOrder();
                }
            }

            data.text.color = (COLOR) ((int) data.text.color).ToHostOrder();

            data.backup.number = data.backup.number.ToHostOrder();

            data.videoStore.maxDay = data.videoStore.maxDay.ToHostOrder();
            data.videoStore.timePerFile = data.videoStore.timePerFile.ToHostOrder();


            return data;
        }
    }
}
